﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using WebComponents.Data;
using WebComponents;
using WebComponents.Security;
using System.Collections.Generic;
using GBIT.Web;
using System.Linq;
using System.Linq.Expressions;

public partial class TinTucList : UserControlBase {
  public string tabid = "";
  private List<ArticleInfo> ArticleInfoList = null;
  protected void Page_Load(object sender, EventArgs e) {
    this.AddModule = "TinTuc/ThemTinTuc.ascx";
    tabid = ToString(Request.Params["tabid"]);
    if (!Page.IsPostBack) {
      cmbChuyenMuc.Items.Add(new ListItem("--Tất cả--", "0"));
      Global.LoadCategory(cmbLanguage.SelectedValue, ref cmbChuyenMuc, CatetoryType.News);
      grvArticleInfo.DataSource = GetNews();
      grvArticleInfo.DataBind();     
    }
    this.EditEnabled = true;
    this.DeleteEnabled = true;
    this.AddEnabled = true;    
    this.Title = "Danh sách tin tức";
  }
  private List<ArticleInfo> GetNews() {
    var kq = ArticleController.GetByCultureID(cmbLanguage.SelectedValue);
    if (kq.Count > 0 && cmbChuyenMuc.SelectedIndex != 0)
      kq = kq.Where(a => a.CateID == ToInt(cmbChuyenMuc.SelectedValue)).ToList();
    if (kq.Count > 0 && cmbStatus.SelectedIndex != 0)
      kq = kq.Where(a => a.StatusID == ToInt(cmbStatus.SelectedValue)).ToList();
    if (kq.Count <= 0) {
      this.EditEnabled = false;
      this.DeleteEnabled = false;
    } else {
      this.EditEnabled = true;
      this.DeleteEnabled = true;
    }
    return kq;
  }
  protected void grvArticleInfo_PageIndexChanging(object sender, GridViewPageEventArgs e) {
    ArticleInfoList = GetNews();
    grvArticleInfo.PageIndex = e.NewPageIndex;
    grvArticleInfo.DataSource = ArticleInfoList;
    grvArticleInfo.DataBind();
  }

  protected void grvArticleInfo_Sorting(object sender, GridViewSortEventArgs e) {
    ArticleInfoList = GetNews();
    ArticleInfoList.Sort(new ObjectComparer<ArticleInfo>(e.SortExpression, ObjectComparer<ArticleInfo>.AutoSortDirection));
    grvArticleInfo.DataSource = ArticleInfoList;
    grvArticleInfo.DataBind();
  }

  protected void cmbLanguage_SelectedIndexChanged(object sender, EventArgs e) {
    grvArticleInfo.DataSource = GetNews();
    grvArticleInfo.DataBind();
  }
  protected override void OnDeleteRecord() {
    foreach (GridViewRow row in grvArticleInfo.Rows) {
      if (((HtmlInputCheckBox)row.FindControl("chkSelect")).Checked) {
        ArticleController.Delete(ConvertType.ToInt(((HtmlInputCheckBox)row.FindControl("chkSelect")).Attributes["title"].ToString()));
      }
    }
    grvArticleInfo.DataSource = GetNews();
    grvArticleInfo.DataBind();
  }
  protected void cmbStatus_SelectedIndexChanged(object sender, EventArgs e) {
    grvArticleInfo.DataSource = GetNews();
    grvArticleInfo.DataBind();
  }
  protected void cmbChuyenMuc_SelectedIndexChanged(object sender, EventArgs e) {
    grvArticleInfo.DataSource = GetNews();
    grvArticleInfo.DataBind();
  }
}
